package com.suixun.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.suixun.entity.page.PageRequest;
import com.suixun.entity.page.PageResultDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.suixun.dao.*;
import com.suixun.dao.model.PageGrid;
import com.suixun.dao.model.PageQueryModel;
import com.suixun.entity.*;
import com.suixun.entity.ext.ExtRptAlarm;
import com.suixun.entity.ext.FullObjectinfo;
import com.suixun.entity.page.DtActiveTrackPage;
import com.suixun.entity.page.ExtRptAlarmPage;
import com.suixun.service.*;
import com.suixun.service.impl.BaseServiceImpl;
import com.suixun.service.common.CommonService;
import com.suixun.service.mongodb.AlarmLogService;
import com.suixun.service.mongodb.UserCacheService;
import com.suixun.service.redis.RedisService;
import com.suixun.util.*;


@Service
public class AlarmServiceImpl extends BaseServiceImpl implements AlarmService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RptAlarmMapper rptAlarmMapper;
    @Resource
    private CommonService commonService;
    @Resource
    private ObjectService objectService;
    
    @Resource
    public AlarmLogService alarmLogService;
    @Resource(name = "redisService")
    private RedisService redisService;
    @Resource
    private StdPeopleObjectMapper stdPeopleObjectMapper;
    /*
     * 获取当前电量
     */
    @Override
    public ExtRptAlarm getCurrentPower(Integer peopleid, Integer objectid) {
        // 分页获取
        Map<String, String> m = new HashMap<String, String>();
        m.put("peopleid", String.valueOf(peopleid));
        m.put("objectid", String.valueOf(objectid));
        m.put("alarmtype", "20");
        PageQueryModel pqm = new PageQueryModel(1, 1, "alarmid", "desc");
        pqm.setDoCount(false);
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sf.parse(sf.format(new Date()));
            Calendar c = Calendar.getInstance();
            c.setTime(startTime);
            c.add(Calendar.DAY_OF_YEAR, 1);
            Date endTime = c.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            m.put("startTime", sdf.format(startTime));
            m.put("endTime", sdf.format(endTime));
        } catch (Exception e) {}
        pqm.setQueryParam(m);
        PageGrid pageGrid =
                this.commonService.pageForObject("RptAlarmExtendSql.getCurrentPower", pqm);
        BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
        List<ExtRptAlarm> ents = null;
        try {
            ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (ents != null && ents.size() > 0) {
            return ents.get(0);
        }
        return null;
    }

    /*
     * 获取当前低电量
     */
    @Override
    public ExtRptAlarm getLowPower(Integer peopleid, Integer objectid, Integer lowpowervalue) {
        // 分页获取
        Map<String, String> m = new HashMap<String, String>();
        m.put("peopleid", String.valueOf(peopleid));
        m.put("objectid", String.valueOf(objectid));
        m.put("alarmvalue", String.valueOf(lowpowervalue));
        m.put("alarmtype", "20");
        PageQueryModel pqm = new PageQueryModel(1, 1, "alarmid", "desc");
        pqm.setDoCount(false);
        pqm.setQueryParam(m);
        PageGrid pageGrid = this.commonService.pageForObject("RptAlarmExtendSql.getLowPower", pqm);
        BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
        List<ExtRptAlarm> ents = null;
        try {
            ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (ents != null && ents.size() > 0) {
            return ents.get(0);
        }
        return null;
    }

    /*
     * 分页获取电量列表
     */
    @Override
    public ExtRptAlarmPage getPowersPaged(Integer peopleid, Integer objectid, Integer pageIndex,
            Integer pageSize) {
        
        // 分页获取
        FullObjectinfo fullObjectinfo = this.objectService.getBabyInfo(peopleid, objectid);
        if (fullObjectinfo != null) {
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            m.put("alarmtype", "20");
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject("RptAlarmExtendSql.getPowerListPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
            extRptAlarmPage.setPowers(ents);
            extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
            extRptAlarmPage.setPagecount(pageGrid.getTotal());
            extRptAlarmPage.setRecordcount(pageGrid.getRecords());
            return extRptAlarmPage;
        }
        return null;
    }
    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'poweralarms' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'lowpoweralarm' + '_' + #objectid"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
    })
    public Boolean deletePower(Integer peopleid, Integer objectid, Integer alarmid) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidEqualTo(alarmid);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    /*
     * 逗号分隔
     */
    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'poweralarms' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'lowpoweralarm' + '_' + #objectid"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
    })
    public Boolean deletePowers(Integer peopleid, Integer objectid, String alarmids) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        List<Integer> arralarmids = StringUtil.convertToInteger(alarmids);
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidIn(arralarmids);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    @Override
    public RptAlarm getSafeRegionAlarm(Integer peopleid, Integer objectid, Integer alarmid) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidEqualTo(alarmid);
        List<RptAlarm> ents = rptAlarmMapper.selectByExample(rptAlarmExample);
        if (ents != null && ents.size() > 0) {
            return ents.get(0);
        }
        return null;
    }

    /*
     * 分页获取电子围栏列表
     */
    @Override
    public ExtRptAlarmPage getSafeRegionAlarmsPaged(Integer peopleid, Integer objectid,
            Integer pageIndex, Integer pageSize) {

        // 分页获取
        FullObjectinfo fullObjectinfo = this.objectService.getBabyInfo(peopleid, objectid);
        if (fullObjectinfo != null) {
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject(
                            "RptAlarmExtendSql.getSafeRegionAlarmListPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage;
            }
            return null;
        }
        return null;
    }

    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'saferegionalarms' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'saferegionalarm' + '_' + #objectid + '_' + #alarmid"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
                    })
    public Boolean deleteSafeRegionAlarm(Integer peopleid, Integer objectid, Integer alarmid) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidEqualTo(alarmid);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    /*
     * 逗号分隔
     */
    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'saferegionalarms' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
    })
    public Boolean deleteSafeRegionAlarms(Integer peopleid, Integer objectid, String alarmids) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        List<Integer> arralarmids = StringUtil.convertToInteger(alarmids);
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidIn(arralarmids);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    /*
     * 分页获取SOS列表
     */
    @Override
    @Cacheable(value="alarmCache",key="'sosalarm' + '_' + #objectid + '_' + #pageIndex")
    public ExtRptAlarmPage getSOSsPaged(Integer peopleid, Integer objectid, Integer pageIndex,
            Integer pageSize) {
        
        FullObjectinfo fullObjectinfo = this.objectService.getBabyInfo(peopleid, objectid);
        if (fullObjectinfo != null) {
            // 分页获取
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject("RptAlarmExtendSql.getSOSListPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage;
            }
            return null;
        }
        return null;
    }

    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'sosalarm' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'sos' + '_' + #objectid + '_' + #alarmid"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
    })
    public Boolean deleteSOS(Integer peopleid, Integer objectid, Integer alarmid) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidEqualTo(alarmid);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    /*
     * 逗号分隔
     */
    @Override
    @Caching(evict={
                    @CacheEvict(value="alarmCache",key="'sosalarm' + '_' + #objectid + '_*'"),
                    @CacheEvict(value="alarmCache",key="'allalarmpage' + '_' + #objectid + '_*'")
    })
    public Boolean deleteSOSs(Integer peopleid, Integer objectid, String alarmids) {
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        List<Integer> arralarmids = StringUtil.convertToInteger(alarmids);
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidIn(arralarmids);
        rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }

    @Override
    @Cacheable(value="alarmCache",key="'currentsos' + '_' + #objectid")
    public ExtRptAlarm getCurrentSOS(Integer peopleid, Integer objectid) {
        
        // 分页获取
        Map<String, String> m = new HashMap<String, String>();
        m.put("peopleid", String.valueOf(peopleid));
        m.put("objectid", String.valueOf(objectid));
        m.put("alarmtype", "1");
        PageQueryModel pqm = new PageQueryModel(1, 1, "alarmid", "desc");
        pqm.setDoCount(false);
        pqm.setQueryParam(m);
        PageGrid pageGrid =
                this.commonService.pageForObject("RptAlarmExtendSql.getSOSListPage", pqm);
        BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
        List<ExtRptAlarm> ents = null;
        try {
            ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (ents != null && ents.size() > 0) {
            return ents.get(0);
        }
        return null;
    }

    @Override
    public ExtRptAlarmPage getAllAlarmsPaged(Integer peopleid, Integer objectid, Integer pageIndex,
            Integer pageSize) {
        StdPeopleObjectExample stdPeopleObjectExample = new StdPeopleObjectExample();
        stdPeopleObjectExample.createCriteria().andPeopleidEqualTo(peopleid)
                .andObjectidEqualTo(objectid);
        List<StdPeopleObject> entsList =
                stdPeopleObjectMapper.selectByExample(stdPeopleObjectExample);
        if (entsList != null && entsList.size() > 0) {
            
            FullObjectinfo fullObjectinfo = objectService.getBabyInfo(peopleid, objectid);
            if (fullObjectinfo == null)
                return null;
            //非管理员
            if(!fullObjectinfo.getPeopleid().equals(peopleid) && entsList.get(0).getFamilystatus().equals("班主任"))
                return null;
            
            // 分页获取
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(entsList.get(0).getCreatetime() != null)
                m.put("startTime", sdf.format(entsList.get(0).getCreatetime()));
            else
                m.put("startTime", "2016-01-01");
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject("RptAlarmExtendSql.getListPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage;
            }
            return null;
        }

        return null;
    }

    @Override
    public ExtRptAlarmPage getAttendanceAlarmsPaged(Integer peopleid, Integer objectid,
            Integer pageIndex, Integer pageSize) {
       
        StdPeopleObjectExample stdPeopleObjectExample = new StdPeopleObjectExample();
        stdPeopleObjectExample.createCriteria().andPeopleidEqualTo(peopleid)
                .andObjectidEqualTo(objectid);
        List<StdPeopleObject> entsList =
                stdPeopleObjectMapper.selectByExample(stdPeopleObjectExample);
        if (entsList != null && entsList.size() > 0) {
            
            FullObjectinfo fullObjectinfo = objectService.getBabyInfo(peopleid, objectid);
            if (fullObjectinfo == null)
                return null;
            //非管理员
            if(!fullObjectinfo.getPeopleid().equals(peopleid) && entsList.get(0).getFamilystatus().equals("班主任"))
                return null;
            
            // 分页获取
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(entsList.get(0).getCreatetime() != null)
                m.put("startTime", sdf.format(entsList.get(0).getCreatetime()));
            else
                m.put("startTime", "2016-01-01");
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService
                            .pageForObject("RptAlarmExtendSql.getAttendanceListPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage;
            }
            return null;
        }
        
        return null;
    }

    @Override
    public ExtRptAlarmPage getNoticesPaged(Integer peopleid, Integer objectid, Integer pageIndex,
            Integer pageSize) {
        
        StdPeopleObjectExample stdPeopleObjectExample = new StdPeopleObjectExample();
        stdPeopleObjectExample.createCriteria().andPeopleidEqualTo(peopleid)
                .andObjectidEqualTo(objectid);
        List<StdPeopleObject> entsList =
                stdPeopleObjectMapper.selectByExample(stdPeopleObjectExample);
        if (entsList != null && entsList.size() > 0) {
            
            FullObjectinfo fullObjectinfo = objectService.getBabyInfo(peopleid, objectid);
            if (fullObjectinfo == null)
                return null;
            //非管理员
            if(!fullObjectinfo.getPeopleid().equals(peopleid) && entsList.get(0).getFamilystatus().equals("班主任"))
                return null;
            // 分页获取
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(entsList.get(0).getCreatetime() != null)
                m.put("startTime", sdf.format(entsList.get(0).getCreatetime()));
            else
                m.put("startTime", "2016-01-01");
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject("RptAlarmExtendSql.getNoticesPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage; 
            }
            return null;
        }
        
        return null;
    }

    @Override
    public ExtRptAlarmPage getCareforPage(Integer peopleid, Integer objectid, Integer pageIndex,
            Integer pageSize) {
        StdPeopleObjectExample stdPeopleObjectExample = new StdPeopleObjectExample();
        stdPeopleObjectExample.createCriteria().andPeopleidEqualTo(peopleid)
                .andObjectidEqualTo(objectid);
        List<StdPeopleObject> entsList =
                stdPeopleObjectMapper.selectByExample(stdPeopleObjectExample);
        if (entsList != null && entsList.size() > 0) {
            
            FullObjectinfo fullObjectinfo = objectService.getBabyInfo(peopleid, objectid);
            if (fullObjectinfo == null)
                return null;
            //非管理员
            if(!fullObjectinfo.getPeopleid().equals(peopleid) && entsList.get(0).getFamilystatus().equals("班主任"))
                return null;
            // 分页获取
           
            Map<String, String> m = new HashMap<String, String>();
            m.put("peopleid", String.valueOf(peopleid));
            m.put("objectid", String.valueOf(objectid));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(entsList.get(0).getCreatetime() != null)
                m.put("startTime", sdf.format(entsList.get(0).getCreatetime()));
            else
                m.put("startTime", "2016-01-01");
            PageQueryModel pqm = new PageQueryModel(pageIndex, pageSize, "alarmid", "desc");
            pqm.setQueryParam(m);
            PageGrid pageGrid =
                    this.commonService.pageForObject("RptAlarmExtendSql.getCareforPage", pqm);
            BeanUtil<ExtRptAlarm> beanUtil = new BeanUtil<ExtRptAlarm>();
            List<ExtRptAlarm> ents = null;
            try {
                ents = beanUtil.ListMap2JavaBean(pageGrid.getRows(), ExtRptAlarm.class);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            if (ents != null && ents.size() > 0) {
                ExtRptAlarmPage extRptAlarmPage = new ExtRptAlarmPage();
                extRptAlarmPage.setPowers(ents);
                extRptAlarmPage.setObjectname(fullObjectinfo.getObjectname());
                extRptAlarmPage.setPagecount(pageGrid.getTotal());
                extRptAlarmPage.setRecordcount(pageGrid.getRecords());
                return extRptAlarmPage; 
            }
            return null;
        }
        return null;
    }

    /*
     * 逗号分隔
     */
    @Override
    @CacheEvict(value="alarmCache",key="#type + '_' + #objectid + '_*'")
    public Boolean deleteAlarms(String type,Integer peopleid, Integer objectid, String alarmids) {
        //atterdancealarm
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        List<Integer> arralarmids = StringUtil.convertToInteger(alarmids);
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmidIn(arralarmids);
        if(type.equals("careforalarm")){
            RptAlarm rptAlarm = new RptAlarm();
            rptAlarm.setIsdeleted(true);
            rptAlarmMapper.updateByExampleSelective(rptAlarm, rptAlarmExample);
        }
        else
            rptAlarmMapper.deleteByExample(rptAlarmExample);
        return true;
    }
    
    @Override
    @Cacheable(value="alarmCache",key="'flower_' + #objectid")
    public int getFlower(Integer objectid){
        RptAlarmExample rptAlarmExample = new RptAlarmExample();
        rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmtypeEqualTo(32);
        return rptAlarmMapper.countByExample(rptAlarmExample);
    }
    
    /**
     * 获取未读信息的 数量
     *  noticesCount    未查看的通知数量
     *  careforCount    未查看的关爱数量
     * @param objectid
     * @return
     */
    @Override
    public Map<String ,Integer> getNotReadMsgCount(Integer objectid ,Integer peopleid){

        Map<String ,Integer> rtn = new HashMap<>();
        String key = "notReadNoticesCount_" + objectid + "_" + peopleid;
        Integer notReadNoticesCount = (Integer)redisService.get(key);
        if(notReadNoticesCount!=null){
            rtn.put("noticesCount",notReadNoticesCount);
        }else{
            RptAlarmExample rptAlarmExample = new RptAlarmExample();
            rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmtypeEqualTo(28).andStatusIsNull();
            int count = rptAlarmMapper.countByExample(rptAlarmExample);
            redisService.put(key,count);
            rtn.put("noticesCount",count);
        }

        String careforKey = "notReadCareforCount_" + objectid + "_" + peopleid;
        Integer notReadCareforCount = (Integer)redisService.get(careforKey);
        if(notReadCareforCount!=null){
            rtn.put("careforCount",notReadCareforCount);
        }else{
            RptAlarmExample rptAlarmExample = new RptAlarmExample();
            rptAlarmExample = new RptAlarmExample();
            List<Integer> types = new ArrayList<>();
            types.add(29);
            types.add(30);
            types.add(31);
            types.add(32);
            types.add(33);
            rptAlarmExample.createCriteria().andObjectidEqualTo(objectid).andAlarmtypeIn(types).andStatusIsNull();
            int count = rptAlarmMapper.countByExample(rptAlarmExample);
            redisService.put(careforKey,count);
            rtn.put("careforCount",count);
        }

        return rtn;
    }

    /**
     * 根据objectid跟消息类型分页查询
     * @param objectId      学生id
     * @param pageRequest   页数
     * @param alarmType     信息类型
     *                      1SOS求助 3进范围报警  4出范围报警 20电量 21考勤到校
     *                      22考勤离校 23考勤到家  24套餐提醒 5进学校 26出学校
     *                      27续费提醒  28老师通知 29老师定位 30老师通话
     * @return
     */
    public PageResultDO<RptAlarm> queryRptAlarmPageByObjectId(Integer objectId, PageRequest pageRequest, List<Integer> alarmType){
        PageResultDO<RptAlarm> rtn = new PageResultDO<>(pageRequest);
        RptAlarmExample example = new RptAlarmExample();
        example.createCriteria().andObjectidEqualTo(objectId).andAlarmtypeIn(alarmType).andIsdeletedIsNull();
        example.or(example.createCriteria().andObjectidEqualTo(objectId).andAlarmtypeIn(alarmType).andIsdeletedEqualTo(false));
        int count = rptAlarmMapper.countByExample(example);
        rtn.setTotalElements(count);
        example.setOrderByClause("alarmid desc limit "+pageRequest.getSize()+" offset "+pageRequest.getOffset());
        rtn.setResult(rptAlarmMapper.selectByExample(example));
        return rtn;
    }
}
